﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using System.Diagnostics;


namespace WebOptiDemo
{
    public partial class default2 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Stopwatch time = new Stopwatch();
            Stopwatch time2 = new Stopwatch();
            Stopwatch time3 = new Stopwatch();


            if (!Page.IsPostBack)
            {
                time.Start(); 
                EquiposRepository repo = new EquiposRepository();
                List<Catalogo> lista = repo.GetCatalogoCombo();
     

                ddlComboXML.DataSource = lista;
                ddlComboXML.DataValueField = "IdCatalogo";
                ddlComboXML.DataTextField = "Parte";
                ddlComboXML.DataBind();

                time.Stop();

                TimeSpan ts = time.Elapsed;


                string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}",
                ts.Hours, ts.Minutes, ts.Seconds,
                ts.Milliseconds / 10);

                txtDemora1.Text = elapsedTime;

                time3.Start();

                List<Catalogo> lista2 = repo.GetCatalogoSinXML();

                ddlComboNormal.DataSource = lista2;
                ddlComboNormal.DataValueField = "IdCatalogo";
                ddlComboNormal.DataTextField = "Parte";
                ddlComboNormal.DataBind();

                time3.Start();

                ts = time3.Elapsed;

                elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}",
             ts.Hours, ts.Minutes, ts.Seconds,
             ts.Milliseconds / 10);

                txtDemora3.Text = elapsedTime;


                time2.Start();


                gvPartes.DataSource = lista2;
                gvPartes.DataBind();

                Cache.Insert("lista", lista2);

                time2.Stop();

                ts = time2.Elapsed;

                elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}",
             ts.Hours, ts.Minutes, ts.Seconds,
             ts.Milliseconds / 10);

                txtDemora2.Text = elapsedTime;
            }
            else
            {
                List<Catalogo> lista = Cache.Get("lista") as List<Catalogo>;
                if (lista !=null)
                {
                    gvPartes.DataSource = lista;
                    gvPartes.DataBind();
                }
            }

            
            
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            Stopwatch time = new Stopwatch();
            
            time.Start();

            List<Catalogo> lista = Cache.Get("lista") as List<Catalogo>;

            var query = from c in lista
                        where c.Parte.ToUpper().Contains(txtCriterio.Text.ToUpper())
                        select c;

            gvPartes.DataSource = query.ToList();
            gvPartes.DataBind();
            
            time.Stop();

            TimeSpan ts = time.Elapsed;



            string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}",
        ts.Hours, ts.Minutes, ts.Seconds,
        ts.Milliseconds / 10);

            txtDemora2.Text = elapsedTime;
        }


        protected void gvPartes_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvPartes.PageIndex = e.NewPageIndex;

            List<Catalogo> lista = Cache.Get("lista") as List<Catalogo>;
            if (lista != null)
            {
                gvPartes.DataSource = lista;
                gvPartes.DataBind();
            }
        }
    }
}
